/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.clinic.patient.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.clinic.patient.entity.PtPatient;
import com.thinkgem.jeesite.modules.clinic.patient.service.PtPatientService;

/**
 * 单表生成Controller
 * @author lizhengheng
 * @version 2018-03-19
 */
@Controller
@RequestMapping(value = "${adminPath}/clinic/ptPatient")
public class PtPatientController extends BaseController {

	@Autowired
	private PtPatientService ptPatientService;
	
	@ModelAttribute
	public PtPatient get(@RequestParam(required=false) String id) {
		PtPatient entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = ptPatientService.get(id);
		}
		if (entity == null){
			entity = new PtPatient();
		}
		return entity;
	}
	
	@RequiresPermissions("clinic:ptPatient:view")
	@RequestMapping(value = {"list", ""})
	public String list(PtPatient ptPatient, HttpServletRequest request, HttpServletResponse response, 
			Model model) {
		Page<PtPatient> page = ptPatientService.findPage(new Page<PtPatient>(request, response), ptPatient); 
		model.addAttribute("page", page);
		model.addAttribute("ptPatient", ptPatient);
		return "modules/clinic/ptPatientList";
	}

	@RequiresPermissions("clinic:ptPatient:view")
	@RequestMapping(value = "form")
	public String form(PtPatient ptPatient, Model model) {
		model.addAttribute("ptPatient", ptPatient);
		return "modules/clinic/ptPatientForm";
	}

	@RequiresPermissions("clinic:ptPatient:edit")
	@RequestMapping(value = "save")
	public String save(PtPatient ptPatient, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, ptPatient)){
			return form(ptPatient, model);
		}
		ptPatientService.save(ptPatient);
		addMessage(redirectAttributes, "保存单表成功");
		return "redirect:"+Global.getAdminPath()+"/clinic/ptPatient/?repage";
	}
	
	@RequiresPermissions("clinic:ptPatient:edit")
	@RequestMapping(value = "delete")
	public String delete(PtPatient ptPatient, RedirectAttributes redirectAttributes) {
		ptPatientService.delete(ptPatient);
		addMessage(redirectAttributes, "删除单表成功");
		return "redirect:"+Global.getAdminPath()+"/clinic/ptPatient/?repage";
	}

}